//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.goldhuman.Common.Marshal;

import com.goldhuman.Common.Octets;

import java.io.UnsupportedEncodingException;

public class OctetsStream extends Octets {
    private static final String JSON_CHAR_SET = "UTF-16LE";
    private static final int MAXSPARE = 16384;
    private int pos = 0;
    private int tranpos = 0;

    public OctetsStream() {
    }

    public OctetsStream(int size) {
        super(size);
    }

    public OctetsStream(Octets o) {
        super(o);
    }

    public static OctetsStream wrap(Octets o) {
        OctetsStream os = new OctetsStream();
        os.swap(o);
        return os;
    }

    public Object clone() {
        OctetsStream os = (OctetsStream)super.clone();
        os.pos = this.pos;
        os.tranpos = this.pos;
        return os;
    }

    public final boolean eos() {
        return this.pos == this.size();
    }

    public final int position(int pos) {
        this.pos = pos;
        return this.pos;
    }

    public final int position() {
        return this.pos;
    }

    public final int remain() {
        return this.size() - this.pos;
    }

    public OctetsStream marshal(byte x) {
        this.push_back(x);
        return this;
    }

    public OctetsStream marshal(boolean b) {
        this.push_back((byte)(b ? 1 : 0));
        return this;
    }

    public OctetsStream marshal(short x) {
        return this.marshal((byte)(x >> 8)).marshal((byte)x);
    }

    public OctetsStream marshal(char x) {
        return this.marshal((byte)(x >> 8)).marshal((byte)x);
    }

    public OctetsStream marshal(int x) {
        return this.marshal((byte)(x >> 24)).marshal((byte)(x >> 16)).marshal((byte)(x >> 8)).marshal((byte)x);
    }

    public OctetsStream marshal(long x) {
        return this.marshal((byte)((int)(x >> 56))).marshal((byte)((int)(x >> 48))).marshal((byte)((int)(x >> 40))).marshal((byte)((int)(x >> 32))).marshal((byte)((int)(x >> 24))).marshal((byte)((int)(x >> 16))).marshal((byte)((int)(x >> 8))).marshal((byte)((int)x));
    }

    public OctetsStream marshal(float x) {
        return this.marshal(Float.floatToRawIntBits(x));
    }

    public OctetsStream marshal(double x) {
        return this.marshal(Double.doubleToRawLongBits(x));
    }

    public OctetsStream compact_uint32(int x) {
        if (x < 64) {
            return this.marshal((byte)x);
        } else if (x < 16384) {
            return this.marshal((short)(x | '耀'));
        } else if (x < 536870912) {
            return this.marshal(x | -1073741824);
        } else {
            this.marshal((byte)-32);
            return this.marshal(x);
        }
    }

    public OctetsStream compact_sint32(int x) {
        if (x >= 0) {
            if (x < 64) {
                return this.marshal((byte)x);
            } else if (x < 8192) {
                return this.marshal((short)(x | '耀'));
            } else if (x < 268435456) {
                return this.marshal(x | -1073741824);
            } else {
                this.marshal((byte)-32);
                return this.marshal(x);
            }
        } else if (-x > 0) {
            x = -x;
            if (x < 64) {
                return this.marshal((byte)(x | 64));
            } else if (x < 8192) {
                return this.marshal((short)(x | 'ꀀ'));
            } else if (x < 268435456) {
                return this.marshal(x | -805306368);
            } else {
                this.marshal((byte)-16);
                return this.marshal(x);
            }
        } else {
            this.marshal((byte)-16);
            return this.marshal(x);
        }
    }

    public OctetsStream marshal(com.goldhuman.Common.Marshal.Marshal m) {
        return m.marshal(this);
    }

    public OctetsStream marshal(Octets o) {
        this.compact_uint32(o.size());
        this.insert(this.size(), o);
        return this;
    }

    public OctetsStream marshal(String str) {
        return this.marshal(str, (String)null);
    }

    public OctetsStream marshal(String str, String charset) {
        try {
            this.marshal(charset == null ? str.getBytes() : str.getBytes(charset));
            return this;
        } catch (Exception var4) {
            throw new RuntimeException(var4);
        }
    }

    public OctetsStream Begin() {
        this.tranpos = this.pos;
        return this;
    }

    public OctetsStream Rollback() {
        this.pos = this.tranpos;
        return this;
    }

    public OctetsStream Commit() {
        if (this.pos >= 16384) {
            this.erase(0, this.pos);
            this.pos = 0;
        }

        return this;
    }

    public byte unmarshal_byte() throws MarshalException {
        if (this.pos + 1 > this.size()) {
            throw new MarshalException();
        } else {
            return this.getByte(this.pos++);
        }
    }

    public boolean unmarshal_boolean() throws MarshalException {
        return this.unmarshal_byte() == 1;
    }

    public short unmarshal_short() throws MarshalException {
        if (this.pos + 2 > this.size()) {
            throw new MarshalException();
        } else {
            byte b0 = this.getByte(this.pos++);
            byte b1 = this.getByte(this.pos++);
            return (short)(b0 << 8 | b1 & 255);
        }
    }

    public char unmarshal_char() throws MarshalException {
        if (this.pos + 2 > this.size()) {
            throw new MarshalException();
        } else {
            byte b0 = this.getByte(this.pos++);
            byte b1 = this.getByte(this.pos++);
            return (char)(b0 << 8 | b1 & 255);
        }
    }

    public int unmarshal_int() throws MarshalException {
        if (this.pos + 4 > this.size()) {
            throw new MarshalException();
        } else {
            byte b0 = this.getByte(this.pos++);
            byte b1 = this.getByte(this.pos++);
            byte b2 = this.getByte(this.pos++);
            byte b3 = this.getByte(this.pos++);
            return (b0 & 255) << 24 | (b1 & 255) << 16 | (b2 & 255) << 8 | (b3 & 255) << 0;
        }
    }

    public long unmarshal_long() throws MarshalException {
        if (this.pos + 8 > this.size()) {
            throw new MarshalException();
        } else {
            byte b0 = this.getByte(this.pos++);
            byte b1 = this.getByte(this.pos++);
            byte b2 = this.getByte(this.pos++);
            byte b3 = this.getByte(this.pos++);
            byte b4 = this.getByte(this.pos++);
            byte b5 = this.getByte(this.pos++);
            byte b6 = this.getByte(this.pos++);
            byte b7 = this.getByte(this.pos++);
            return ((long)b0 & 255L) << 56 | ((long)b1 & 255L) << 48 | ((long)b2 & 255L) << 40 | ((long)b3 & 255L) << 32 | ((long)b4 & 255L) << 24 | ((long)b5 & 255L) << 16 | ((long)b6 & 255L) << 8 | ((long)b7 & 255L) << 0;
        }
    }

    public float unmarshal_float() throws MarshalException {
        return Float.intBitsToFloat(this.unmarshal_int());
    }

    public double unmarshal_double() throws MarshalException {
        return Double.longBitsToDouble(this.unmarshal_long());
    }

    public int uncompact_uint32() throws MarshalException {
        if (this.pos == this.size()) {
            throw new MarshalException();
        } else {
            switch (this.getByte(this.pos) & 224) {
                case 128:
                case 160:
                    return this.unmarshal_short() & 32767;
                case 192:
                    return this.unmarshal_int() & 1073741823;
                case 224:
                    this.unmarshal_byte();
                    return this.unmarshal_int();
                default:
                    return this.unmarshal_byte();
            }
        }
    }

    public int uncompact_sint32() throws MarshalException {
        if (this.pos == this.size()) {
            throw new MarshalException();
        } else {
            switch (this.getByte(this.pos) & 240) {
                case 64:
                case 80:
                case 96:
                case 112:
                    return -(this.unmarshal_byte() & -65);
                case 128:
                case 144:
                    return this.unmarshal_short() & 32767;
                case 160:
                case 176:
                    return -(this.unmarshal_short() & 24575);
                case 192:
                    return this.unmarshal_int() & 1073741823;
                case 208:
                    return -(this.unmarshal_int() & 805306367);
                case 224:
                    this.unmarshal_byte();
                    return this.unmarshal_int();
                case 240:
                    this.unmarshal_byte();
                    return -this.unmarshal_int();
                default:
                    return this.unmarshal_byte();
            }
        }
    }

    public Octets unmarshal_Octets() throws MarshalException {
        int size = this.uncompact_uint32();
        if (this.pos + size > this.size()) {
            throw new MarshalException();
        } else {
            Octets o = new Octets(this, this.pos, size);
            this.pos += size;
            return o;
        }
    }

    public byte[] unmarshal_bytes() throws MarshalException {
        int size = this.uncompact_uint32();
        if (this.pos + size > this.size()) {
            throw new MarshalException();
        } else {
            byte[] copy = new byte[size];
            System.arraycopy(this.array(), this.pos, copy, 0, size);
            this.pos += size;
            return copy;
        }
    }

    public OctetsStream marshal(byte[] bytes) {
        this.compact_uint32(bytes.length);
        this.insert(this.size(), bytes);
        return this;
    }

    public String unmarshalJson() throws MarshalException {
        try {
            return new String(this.array(), 0, this.size(), "UTF-16LE");
        } catch (UnsupportedEncodingException var2) {
            var2.printStackTrace();
            throw new MarshalException();
        }
    }

    public OctetsStream marshalJson(String str) {
        try {
            this.insert(this.size(), str.getBytes("UTF-16LE"));
            return this;
        } catch (UnsupportedEncodingException var3) {
            var3.printStackTrace();
            throw new RuntimeException(var3);
        }
    }

    public OctetsStream unmarshal(Octets os) throws MarshalException {
        int size = this.uncompact_uint32();
        if (this.pos + size > this.size()) {
            throw new MarshalException();
        } else {
            os.replace(this, this.pos, size);
            this.pos += size;
            return this;
        }
    }

    public String unmarshal_String() throws MarshalException {
        return this.unmarshal_String((String)null);
    }

    public String unmarshal_String(String charset) throws MarshalException {
        try {
            int size = this.uncompact_uint32();
            if (this.pos + size > this.size()) {
                throw new MarshalException();
            } else {
                int cur = this.pos;
                this.pos += size;
                return charset == null ? new String(this.array(), cur, size) : new String(this.array(), cur, size, charset);
            }
        } catch (Exception var4) {
            throw new MarshalException();
        }
    }

    public OctetsStream unmarshal(Marshal m) throws MarshalException {
        return m.unmarshal(this);
    }
}
